﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class PersonIdentityMapping
    {
        public static PersonIdentity Map(PersonIdentityDTO data)
        {
            PersonIdentity newItem = new PersonIdentity();

            newItem.BirthDate = data.BirthDate;
            newItem.BloodGroup = data.BloodGroup.HasValue ? (int?)data.BloodGroup : (int?)null;
            newItem.DriverLicenceGivenDate = data.DriverLicenceGivenDate;
            newItem.FatherName = data.FatherName;
            newItem.Gender = data.Gender.HasValue ? (int?)data.Gender : (int?)null;            
            newItem.HasDriverLicence = data.HasDriverLicence;
            newItem.Id = data.Id;
            newItem.ImageId = data.ImageId;
            newItem.MartialStatus = data.MartialStatus.HasValue ? (int?)data.MartialStatus : (int?)null;
            newItem.MotherName = data.MotherName;
            newItem.Name = data.Name;
            newItem.PersonId = data.PersonId;
            newItem.PleaceOfBirth = data.PlaceOfBirthId;
            newItem.Surname = data.Surname;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static PersonIdentityDTO Map(PersonIdentity data)
        {
            PersonIdentityDTO newItem = new PersonIdentityDTO();

            newItem.BirthDate = data.BirthDate;
            newItem.BloodGroup = (EBloodGroup?)data.BloodGroup;
            newItem.DriverLicenceGivenDate = data.DriverLicenceGivenDate;
            newItem.FatherName = data.FatherName;
            newItem.Gender = (EGender)data.Gender;
            newItem.HasDriverLicence = data.HasDriverLicence;
            newItem.Id = data.Id;
            newItem.ImageId = data.ImageId;
            newItem.MartialStatus = (EMartialStatus)data.MartialStatus;
            newItem.MotherName = data.MotherName;
            newItem.Name = data.Name;
            newItem.PersonId = data.PersonId;
            newItem.PlaceOfBirthId = data.PleaceOfBirth;
            newItem.Surname = data.Surname;

            return newItem;
        }

        public static PersonIdentity[] Map(PersonIdentityDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static PersonIdentityDTO[] Map(PersonIdentity[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
